import { Injectable } from '@angular/core';
import { NzModalService, NzNotificationService, NzMessageService } from 'ng-zorro-antd';
import { reverse } from 'dns';
import { reject } from 'q';

@Injectable()
export class AbpMessage {

    constructor(private modalService: NzModalService,
        private messageService: NzMessageService) {
    }

    init() {

        this.initMessage();
        this.initNotify();
    }

    initNotify() {
        abp.notify.info = (message: string, title?: string) => {
            this.messageService.create('info', message);
        };

        // 提醒
        abp.notify.success = (message: string, title?: string) => {
            this.messageService.create('success', message);
        };

        abp.notify.warn = (message: string, title?: string) => {
            this.messageService.create('warn', message);
        };
        abp.notify.error = (message: string, title?: string) => {
            this.messageService.create('error', message);
        };
    }

    initMessage() {
        abp.message.info = (message, title) => {
            const dispalyTitle = title == null ? message : title;
            this.modalService.info({
                nzTitle: dispalyTitle,
                nzContent: message,
                nzOnOk: () => console.log('Info OK')
            });
        };

        // 弹出框提示
        abp.message.success = (message, title) => {
            const dispalyTitle = title == null ? message : title;
            this.modalService.success({
                nzTitle: dispalyTitle,
                nzContent: message
            });
        };

        abp.message.warn = (message, title) => {
            const dispalyTitle = title == null ? message : title;
            this.modalService.warning({
                nzTitle: dispalyTitle,
                nzContent: message
            });
        };

        abp.message.error = (message, title) => {
            const dispalyTitle = title == null ? message : title;
            this.modalService.error({
                nzTitle: dispalyTitle,
                nzContent: message
            });
        };


        abp.message.confirm = (message: string, titleOrCallback?: string | ((result: boolean) => void), callback?: (result: boolean) => void, isHtml?: boolean) => {
            let dispalyTitle = message;
            if (typeof titleOrCallback === 'string') {
                dispalyTitle = titleOrCallback == null ? message : titleOrCallback;
            } else if (typeof titleOrCallback === 'function') {
                callback = titleOrCallback;
            }
            this.modalService.confirm({
                nzTitle: dispalyTitle,
                nzContent: message,
                nzOnOk: () => {
                    console.log('ok');
                    if (callback && typeof callback === 'function') {
                        callback(true);
                    }
                },
                nzOnCancel: () => {
                    console.log('cancel');
                    if (callback && typeof callback === 'function') {
                        callback(false);
                    }
                }
            });
        };
    }
}